package com.yj_app.qualitytest;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioGroup;

import androidx.annotation.IdRes;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.widget.PopupWindowCompat;

import com.rengwuxian.materialedittext.MaterialEditText;
import com.yj_app.qualitytest.adapter.TestAdapter;
import com.yj_app.qualitytest.base.BaseActivity;
import com.yj_app.qualitytest.widget.poplist.CommonPopupWindow;
import com.yj_app.qualitytest.widget.poplist.HorizontalPosition;
import com.yj_app.qualitytest.widget.poplist.SmartPopupWindow;
import com.yj_app.qualitytest.widget.poplist.VerticalPosition;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity {


    CommonPopupWindow mWindow;
    TestAdapter testAdapter;
    ListView listView;
    private int mGravity = Gravity.START;
    private int mOffsetX = 0;
    private int mOffsetY = 0;

    private boolean useSmartPopup = true;


    @BindView(R.id.line_edite)
    EditText lineEdit;

    private View mPopupContentView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        initView2();
        initEvent();
    }

    private void initEvent() {
        lineEdit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b){
                   showPopup2();
                }else{

                }

            }
        });

    }

    private void initView2() {
        mPopupContentView =  View.inflate(this, R.layout.layout_pop, null);;
        listView = mPopupContentView.findViewById(R.id.my_llist);
        testAdapter = new TestAdapter(this, getData(), R.layout.item_test);
        listView.setAdapter(testAdapter);

        mWindow = new CommonPopupWindow(this,mPopupContentView);

        View contentView = mWindow.getContentView();
        //需要先测量，PopupWindow还未弹出时，宽高为0
//        contentView.measure(makeDropDownMeasureSpec(mWindow.getWidth()), makeDropDownMeasureSpec(mWindow.getHeight()));



    }

    private void showPopup2() {
        PopupWindowCompat.showAsDropDown(mWindow, lineEdit, mOffsetX, mOffsetY, mGravity);
    }



    @SuppressWarnings("ResourceType")
    private static int makeDropDownMeasureSpec(int measureSpec) {
        int mode;
        if (measureSpec == ViewGroup.LayoutParams.WRAP_CONTENT) {
            mode = View.MeasureSpec.UNSPECIFIED;
        } else {
            mode = View.MeasureSpec.EXACTLY;
        }
        return View.MeasureSpec.makeMeasureSpec(View.MeasureSpec.getSize(measureSpec), mode);
    }

    private List<String> getData() {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            list.add(i + "");
        }
        return list;
    }
}
